#------------------------------------------------------------------------------
# Project:     Multi-Programmer Game Demo
# Name:        Ball.py
# Purpose:     Bouncing Ball Object
# 
# Authors:     Chris Spooner, Chris La-Roo
# Main Author: Chris Spooner
#
# Created:     11/30/2012
# Copyright:   (c) Chris Spooner 2012
# Licence:     New BSD
#------------------------------------------------------------------------------
import pygame, math, sys, random

class Ball():
    def __init__(self, speed, position):
        self.surfaces = []
        self.surfaces += [pygame.image.load("rsc/ball/ball1-1.png")]
        self.surfaces += [pygame.image.load("rsc/ball/ball1-2.png")]
        self.frame = 0
        self.maxFrame = len(self.surfaces)-1
        self.surface = self.surfaces[self.frame]
        self.rect = self.surface.get_rect()
        self.radius = (self.rect.width/2 +self.rect.height/2)/2         
        self.speed = speed
        self.place(position)           
        self.living = True
        if pygame.mixer:
            self.bounceSound = pygame.mixer.Sound("bounce.wav")
        
    def  __str__(self):
        return "I'm a ball " + str(self.rect.center) + str(self.speed) + str(self.living)
     
    def place(self, position):
        print "I've moved to", position
        self.rect.center = position
        
    def move(self):
        self.rect = self.rect.move(self.speed)
        #taken from Ball.py
    
    def distToPoint(self, pt):
        x1 = self.rect.center[0]
        x2 = pt[0]
        y1 = self.rect.center[1]
        y2 = pt[1]
        return math.sqrt(((x2-x1)**2)+((y2-y1)**2))        
    
    def collideScreen(self, screenWidth, screenHeight):
        if (self.rect.left < 0 
            or self.rect.right > screenWidth):
            self.speed[0] = self.speed[0]*-1
        if (self.rect.top < 0 
            or self.rect.bottom > screenHeight):
            self.speed[1] = self.speed[1]*-1
        #Taken from my Ball.py Class
        #combined colide floor and collide wall
                    
    def collidePaddle(self, paddle):
        print "trying to hit paddle", str(paddle)
        if (self.rect.right > paddle.rect.left 
            and self.rect.left < paddle.rect.right):
            if (self.rect.bottom > paddle.rect.top and 
                self.rect.top < paddle.rect.bottom):
                if (self.distToPoint(paddle.rect.center)
                    < self.radius + paddle.radius):                 
                    self.speed[0] = self.speed[0] * -1
                    self.speed[1] = self.speed[1] * -1
                    paddle.speed[0] = paddle.speed[0] * -1
                    paddle.speed[1] = paddle.speed[1] * -1
                    self.living = False   
        
    def collideBall(self, other):
        if (self.rect.right > other.rect.left 
            and self.rect.left < other.rect.right):
            if (self.rect.bottom > other.rect.top and 
                self.rect.top < other.rect.bottom):
                if (self.distToPoint(other.rect.center)
                    < self.radius + other.radius):                    
                    self.speed[0] = self.speed[0] * -1
                    self.speed[1] = self.speed[1] * -1
                    other.speed[0] = other.speed[0] * -1
                    other.speed[1] = other.speed[1] * -1
        #Taken from Ball.py            